package com.zhangye.test.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangye.test.model.Admin;
import com.zhangye.test.model.SearchLog;
import com.zhangye.test.service.AdminService;
import com.zhangye.test.service.EasyArService;
import com.zhangye.test.service.SafeSessionService;
import com.zhangye.test.service.SearchLogService;
import com.zhangye.test.util.PageInfo;

@Controller
@RequestMapping("api/searchLog")
public class SearchLogApi {


	@Autowired
	private SafeSessionService safeSessionService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private SearchLogService searchLogService;
	@Autowired
	private EasyArService easyArService;
	
	
	/**
	 * 搜索结果
	 * 作者： 张叶
	 * 创建时间：2018年7月11日  上午10:53:31
	 */
	@ResponseBody
    @RequestMapping(value="/resultListNew",method=RequestMethod.POST)
	public ApiResult resultListNew(HttpServletRequest request,Integer page, Integer limit,String keywords,String type){
		ApiResult result = new ApiResult();
		if(keywords != null && !"".equals(keywords)
				&& type != null && !"".equals(type)){
			// 分页信息
			PageInfo pageInfo = new PageInfo();
			if (pageInfo.getPage() < 2
					|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
				pageInfo.setCount(0);
			}
			if (limit != null) {// 每页显示条数
				pageInfo.setPageSize(limit);
			}
			if (page != null) {// 当前页
				pageInfo.setPage(page);
			}
			Admin admin = safeSessionService.getCurrent(request);
			List<Map<String, Object>> list = easyArService.selectResultNew(pageInfo, keywords, type, admin);
			if(list.size() > 0){
				result.data.put("ar", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		return result;
	}
	
	
	/**
	 * 我搜索过的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午10:25:25
	 */
	@ResponseBody
	@RequestMapping("/getMySearch")
	public ApiResult getMySearch(HttpServletRequest request,String type){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			String where = " and admin_id = " + admin.getId();
			if(type == null || "".equals(type) || "0".equals(type)){
				where += " and type = '0'";
			}else{
				where += " and type = '1'";
			}
			List<SearchLog> list = searchLogService.selectByWhere(where);
			if(list.size() > 0){
				result.data.put("search", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无记录";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
	
	
	/**
	 * 搜索过的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午10:25:25
	 */
	@ResponseBody
	@RequestMapping("/getWebSearch")
	public ApiResult getWebSearch(HttpServletRequest request,String type){
		ApiResult result = new ApiResult();
		String where = "";
		if(type == null || "".equals(type) || "0".equals(type)){
			where += " and type = '0'";
		}else{
			where += " and type = '1'";
		}
		where += "  order by created_at desc limit 5";
		List<SearchLog> list = searchLogService.selectByWhere(where);
		if(list.size() > 0){
			result.data.put("search", list);
			result.msg.status = 0;
			result.msg.desc = "获取成功";
		}else{
			result.msg.status = 1;
			result.msg.desc = "暂无记录";
		}
		return result;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	/**
	 * 搜索结果
	 * 作者： 张叶
	 * 创建时间：2018年7月11日  上午10:53:31
	 */
	@ResponseBody
    @RequestMapping(value="/resultList",method=RequestMethod.POST)
	public ApiResult resultList(HttpServletRequest request,Integer page, Integer limit,String keywords,String type){
		ApiResult result = new ApiResult();
		if(keywords != null && !"".equals(keywords)
				&& type != null && !"".equals(type)){
			// 分页信息
			PageInfo pageInfo = new PageInfo();
			if (pageInfo.getPage() < 2
					|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
				pageInfo.setCount(0);
			}
			if (limit != null) {// 每页显示条数
				pageInfo.setPageSize(limit);
			}
			if (page != null) {// 当前页
				pageInfo.setPage(page);
			}
			Admin admin = safeSessionService.getCurrent(request);
			List<Map<String, Object>> list = easyArService.selectResult(pageInfo, keywords, type, admin);
			if(list.size() > 0){
				result.data.put("resultList", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无数据";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		return result;
	}
}
